
# 读取中文文字

# 方法1. 把中文以字节串的形式读取。再用字节串的.decode()方法解码
with open('gb2312.txt', 'rb') as fr:
    content = fr.read()
    print(content)
    # 将 GB系列的字节串转化为python的字符串
    # s = content.decode('gb2312')
    # s = content.decode('gbk')
    s = content.decode('gb18030')
    print(s)

with open('utf8.txt', 'rb') as fr:
    content = fr.read()
    print(content)
    # 将 UTF-8系列的字节串转化为python的字符串(UNICODE)
    s = content.decode('utf-8')
    print(s)

print('----------------------------------')
# 方法2. 再open函数中,指定字节串解码为字符的方式。直接以文本的格式操作文件
with open('gb2312.txt', 'r', encoding='gbk') as fr:
    s = fr.read()
    print(s)

with open('utf8.txt', 'r', encoding='utf-8') as fr:
    s = fr.read()
    print(s)
